package com.kun.companion.core.aop.interceptor;

import net.bytebuddy.implementation.bind.annotation.SuperCall;

import java.util.concurrent.Callable;
import java.util.function.Function;

/**
 * TODO
 *
 * @author gzc
 * @since 2025/2/2 5:42
 **/
public class LazyInterceptor {
    private final String beanName;
    private volatile Object realInstance;

    public LazyInterceptor(String beanName) {
        this.beanName = beanName;
    }

    public Object intercept(@SuperCall Callable<?> zuper, Function<Object, Object> function) throws Exception {
        if (realInstance == null) {
            synchronized (this) {
                if (realInstance == null) {
//                    realInstance = doGetBean(beanName, beanDefinitions.get(beanName));
                    realInstance = function.apply(beanName);
                }
            }
        }
        return realInstance;
    }
}
